package cn.cleanarch.infra.idp.pms.controller;

import cn.cleanarch.infra.idp.commons.domain.dto.PageResult;
import cn.cleanarch.infra.idp.pms.domain.dto.DesignFileDTO;
import cn.cleanarch.infra.idp.pms.domain.dto.DesignFileVersionDTO;
import cn.cleanarch.infra.idp.pms.domain.dto.DesignFileVersionPreviewDTO;
import cn.cleanarch.infra.idp.pms.domain.entity.DesignFile;
import cn.cleanarch.infra.idp.pms.domain.query.DesignFileVersionQuery;
import cn.cleanarch.infra.idp.pms.service.DesignFileService;
import cn.cleanarch.infra.idp.web.model.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设计文件版本控制器
 */
@RestController
@RequestMapping("/pms/design-file-version")
public class DesignFileVersionController {

    @Autowired
    private DesignFileService designFileService;

    /**
     * 分页查询设计文件版本
     */
    @GetMapping
    public Result pageQuery(DesignFileVersionQuery query) {
        PageResult<DesignFileVersionDTO> pageResult = designFileService.pageQueryVersion(query);
        return Result.data(pageResult);
    }

    /**
     * 获取设计文件的所有版本
     */
    @GetMapping("/file/{fileId}")
    public Result getVersionsByFileId(@PathVariable("fileId") Long fileId) {
        List<DesignFileVersionDTO> versions = designFileService.getVersionsByFileId(fileId);
        return Result.data(versions);
    }

    /**
     * 根据文件ID和版本号获取特定版本
     */
    @GetMapping("/file/{fileId}/version/{versionNumber}")
    public Result getVersionByFileIdAndVersionNumber(
            @PathVariable("fileId") Long fileId,
            @PathVariable("versionNumber") String versionNumber) {
        DesignFileVersionDTO version = designFileService.getVersionByFileIdAndVersionNumber(fileId, versionNumber);
        return Result.data(version);
    }

    /**
     * 创建新的文件版本
     */
    @PostMapping("/file/{fileId}")
    public Result createVersion(@PathVariable("fileId") Long fileId, @RequestBody DesignFileVersionDTO versionDTO) {
        DesignFileVersionDTO result = designFileService.createVersion(fileId, versionDTO);
        return Result.data(result);
    }

    /**
     * 更新文件版本信息
     */
    @PutMapping("/{versionId}")
    public Result updateVersion(@PathVariable("versionId") Long versionId, @RequestBody DesignFileVersionDTO versionDTO) {
        DesignFileVersionDTO result = designFileService.updateVersion(versionId, versionDTO);
        return Result.data(result);
    }

    /**
     * 删除文件版本
     */
    @DeleteMapping("/{versionId}")
    public Result deleteVersion(@PathVariable("versionId") Long versionId) {
        designFileService.deleteVersion(versionId);
        return Result.success();
    }

    /**
     * 恢复到指定版本
     */
    @PostMapping("/{fileId}/restore/{versionNumber}")
    public Result restoreToVersion(
            @PathVariable("fileId") Long fileId,
            @PathVariable("versionNumber") String versionNumber) {
        DesignFileDTO result = designFileService.restoreToVersion(fileId, versionNumber);
        return Result.data(result);
    }

    /**
     * 优化预览功能：直接通过版本ID进行预览
     */
    @GetMapping("/{versionId}/preview")
    public Result previewVersionById(@PathVariable("versionId") Long versionId) {
        // 从版本表直接获取版本信息
        DesignFileVersionDTO version = designFileService.getVersionById(versionId);
        if (version == null) {
            return Result.fail("版本不存在");
        }

        // 根据版本数据生成预览信息
        DesignFileVersionPreviewDTO previewData = designFileService.generateVersionPreviewData(versionId);
        
        return Result.data(previewData);
    }
}